Iterating different datatypes using For loop
Iterating over a list:
Iterating over string using for loop in python
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
Output
apple
banana
cherry
Iterating over a string:
Iterating over string using for loop in python
for char in "Tutorialsbox":
print(char)
Output
T
u
t
o
r
i
a
l
s
b
o
x
Iterating over a dictionary:
Iterating over dictionary using for loop in python
person = {"name": "Alice", "age": 25}
for key in person:
print(key, person[key])
Output
name Alice
age 25
Iterating over a set:
Iterating over set datatype using for loop in python
unique_numbers = {1, 2, 3, 2, 1}
for num in unique_numbers:
print(num)
Output
1
2
3
Iterating over a tuple:
Iterating tuple using for loop example in python
coordinates = (1, 2, 3)
for coordinate in coordinates:
print(coordinate)
Output
1
2
3
In each of these examples, the for loop goes through each item in the iterable (list, string, dictionary, set, or tuple) and executes the code block for each item.